package service

import (
	"gomain/app/model"

	"github.com/gogf/gf/v2/frame/g"
)

var Category = categoryService{}

type categoryService struct{}

func (s *categoryService) List(page, limit int) ([]model.Category, int, error) {
	var categories []model.Category

	m := g.DB().Model("categories")
	count, err := m.Count()
	if err != nil {
		return nil, 0, err
	}

	err = m.Page(page, limit).Scan(&categories)
	return categories, count, err
}

func (s *categoryService) Add(category *model.Category) error {
	_, err := g.DB().Model("categories").Data(category).Insert()
	return err
}

func (s *categoryService) Update(category *model.Category) error {
	_, err := g.DB().Model("categories").Data(category).Where("id=?", category.Id).Update()
	return err
}

func (s *categoryService) Delete(id int) error {
	_, err := g.DB().Model("categories").Where("id=?", id).Delete()
	return err
}
